package com.itheima.app.controller;

import com.itheima.app.manager.UserManager;
import com.itheima.domain.db.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserManager userManager;

    // 保存用户
    @PostMapping("/user/save")
    public ResponseEntity save(@RequestBody User user) {
        return userManager.save(user);
    }

    // 根据手机号查询
    @GetMapping("/user/findByPhone")
    public ResponseEntity findByPhone(String phone) {
        return userManager.findByPhone(phone);
    }

    // 发送验证码
    @PostMapping("/user/login")
    public void sendSmsCode(@RequestBody Map<String, String> map) {
        // 1.接收参数
        String phone = map.get("phone");
        // 2.调用manager处理
        userManager.sendSmsCode(phone);
    }

    // 注册、登录
    @PostMapping("/user/loginVerification")
    public ResponseEntity regAndLogin(@RequestBody Map<String, String> map){
        // 1.接收参数
        String phone = map.get("phone");
        String verificationCode = map.get("verificationCode");
        // 2.调用mananger并返回结果
        return userManager.regAndLogin(phone,verificationCode);
    }
}
